﻿using Microsoft.DotNet.PlatformAbstractions;

namespace ApiTemplate.WebApi
{
    /// <summary>
    /// 属性注入
    /// </summary>
    public static class PropertiesAutowiredSetup
    {
        /// <summary>
        /// 属性注入
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="appSetting"></param>
        public static void RegisterProperties(this ContainerBuilder builder, AppSetting appSetting)
        {
            string basePath = ApplicationEnvironment.ApplicationBasePath;
            Type controllerBaseType = typeof(ControllerBase);
            builder.RegisterAssemblyTypes(typeof(Program).Assembly)
                .Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType)
                .PropertiesAutowired();

            string dalFile = Path.Combine(basePath, "ApiTemplate.DAL.dll");
            var assemblysDal = System.Reflection.Assembly.LoadFrom(dalFile);
            builder.RegisterAssemblyTypes(assemblysDal)
                .AsImplementedInterfaces()
                .InstancePerDependency()
                .PropertiesAutowired();

            string appFile = Path.Combine(basePath, "ApiTemplate.Application.dll");
            var assemblysApp = System.Reflection.Assembly.LoadFrom(appFile);
            builder.RegisterAssemblyTypes(assemblysApp)
                .AsImplementedInterfaces()
                .InstancePerDependency()
                .PropertiesAutowired();

        }
    }
}
